package cn.wostore.wxhelper.utils;

import cn.wostore.wxhelper.bean.Group;
import cn.wostore.wxhelper.bean.User;
import cn.wostore.wxhelper.utils.WxDatabaseWrapper;
import net.sqlcipher.Cursor;
import net.sqlcipher.database.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wuch
 * @date 2018/12/27 0027
 */
public class WXInfoHelper {
    private final SQLiteDatabase db;
    public WXInfoHelper() {
        db = WxDatabaseWrapper.getInstance().getDatabase();
    }

    /**
     * 获取所有群的信息
     *
     * @return 所有群的详细信息
     */
    public List<Group> getGroups() {
        List<Group> list = new ArrayList<>();
        String sql = "SELECT C.username AS username, C.memberlist AS memberlist,\n" +
                "C.displayname AS displayname,C.roomowner AS roomowner, \n" +
                "C.nickname AS nickname, D.reserved2 AS reserved2\n" +
                "from\n" +
                "(SELECT A.chatroomname AS username, A.memberlist AS memberlist,\n" +
                "A.displayname AS displayname,A.roomowner AS roomowner,B.nickname AS nickname\n" +
                "from\n" +
                "(SELECT chatroomname,memberlist,displayname,roomowner from chatroom) AS A\n" +
                "left join\n" +
                "(SELECT username, nickname from rcontact) AS B\n" +
                "on\n" +
                "A.chatroomname = B.username) AS C\n" +
                "left join\n" +
                "(SELECT username, reserved2 from img_flag) AS D\n" +
                "on C.username = D.username\n";
        Cursor cursor = db.rawQuery(sql, null);
        while (cursor.moveToNext()) {
            Group group = new Group();
            group.setChatroomId(cursor.getString(cursor.getColumnIndex("username")));
            group.setChatroomName(cursor.getString(cursor.getColumnIndex("nickname")));
            group.setImgUrl(cursor.getString(cursor.getColumnIndex("reserved2")));
            String[] memberIdList = cursor.getString(cursor.getColumnIndex("memberlist")).split(";");
            String[] memberNameList = cursor.getString(cursor.getColumnIndex("displayname")).split("、");
            String ownerId = cursor.getString(cursor.getColumnIndex("roomowner"));
            group.setMembers(members(memberIdList, memberNameList, group, ownerId));
            list.add(group);
        }
        cursor.close();
        return list;
    }

    /**
     * 利用查询到的群成员信息构建群成员实体集合，并设置群主
     * @param memberIdList  群成员id组合
     * @param memberNameList    群成员昵称组合
     * @param group 群组实例
     * @param ownerId   群主id
     * @return  群成员集合
     */
    private List<User> members(String[] memberIdList, String[] memberNameList, Group group, String ownerId) {
        List<User> memberList = new ArrayList<>();

        for (int i = 0; i < memberIdList.length; i++) {
            User user = new User();
            user.setUserId(memberIdList[i]);
            user.setUserName(memberNameList[i]);
            memberList.add(user);
            if (memberIdList[i].equals(ownerId)){
                User owner = new User();
                user.setUserId(memberIdList[i]);
                user.setUserName(memberNameList[i]);
                group.setOwner(owner);
            }
        }

        return memberList;
    }

    /**
     * 获取群的群主
     *
     * @param chatroomId 群ID
     * @return 群主信息实体类
     */
    private User getOwner(String chatroomId) {
        Cursor cursor = db.query("chatroom", new String[]{"roomowner"},
                "chatroomname='" + chatroomId + "'", null, null, null, null);
        User user = new User();
        if (cursor.moveToFirst()) {
            user.setUserId(cursor.getString(cursor.getColumnIndex("roomowner")));
        }
        cursor.close();
        return user;
    }

    /**
     * 获取群成员
     *
     * @param chatroomId 群ID
     * @param ownerId    群主ID
     * @param group      群信息
     * @return 群成员列表
     */
    private List<User> getMenmbers(String chatroomId, String ownerId, Group group) {
        Cursor cursor = db.query("chatroom", new String[]{"memberlist", "displayname"},
                "chatroomname='" + chatroomId + "'", null, null, null, null);
        List<User> memberList = new ArrayList<>();

        if (cursor.moveToFirst()) {
            String[] memberIdList = cursor.getString(cursor.getColumnIndex("memberlist")).split(";");
            String[] memberNameList = cursor.getString(cursor.getColumnIndex("displayname")).split("、");

            for (int i = 0; i < memberIdList.length; i++) {
                User user = new User();
                user.setUserId(memberIdList[i]);
                user.setUserName(memberNameList[i]);
                memberList.add(user);
                if (memberIdList[i].equals(ownerId))
                    group.getOwner().setUserName(memberNameList[i]);
            }
        }

        cursor.close();
        return memberList;
    }

}
